package com.nm.web.controller;

import com.nm.commons.exception.SupperException;
import com.nm.orm.pojo.CategoryEntity;
import com.nm.service.CategorysService;
import com.nm.service.output.CategoryOutput;
import com.nm.web.convert.CategoryConvertor;
import com.nm.web.request.CategoryUpdateRequest;
import com.nm.web.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 分类管理
 */
@Controller
@RequestMapping(path = "/category")
public class CategoryController extends BaseController {

    @Autowired
    @Qualifier("categorysService")
    private CategorysService categorysService;

    /**
     * 分类视图
     * @return
     */
    @RequestMapping(path = "")
    public ModelAndView index() {
        ModelAndView model = new ModelAndView();
        model.setViewName("/admin/category/index");
        return model;
    }

    /**
     * 查询所有分类
     * @return
     */
    @RequestMapping(path = "/all", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<List<CategoryOutput>> findAll()
            throws SupperException {

        List<CategoryOutput> output = categorysService.findAll();

        return getResult(output);
    }

    /**
     * 增加菜单视图
     */
    @RequestMapping(path = "/add", method = RequestMethod.GET)
    public ModelAndView add() throws SupperException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/category/info");
        modelAndView.addObject("title", "增加分类");
        return modelAndView;
    }

    /**
     * 查新更新对象 展示修改视图
     * @param id 菜单ID
     * @return
     */
    @RequestMapping(path = "/update/{id}", method = RequestMethod.GET)
    public ModelAndView update(@PathVariable("id") String id)
            throws SupperException {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/admin/category/info");
        modelAndView.addObject("title", "修改分类");

        CategoryEntity entity = categorysService.getById(id);

        modelAndView.addObject("categorys",
                new Result(entity));
        return modelAndView;
    }

    /**
     * 增加修改菜单数据提交
     */
    @RequestMapping(path = "/datapost", method = RequestMethod.POST)
    @ResponseBody
    public ResponseEntity<Result> dataPost(
            @RequestBody CategoryUpdateRequest categorysRequest) throws SupperException {
        if ("".equals(categorysRequest.getId())) {
            categorysRequest.setId(null);
        }
        categorysService.update(CategoryConvertor.conv(categorysRequest));
        return getResult(null);
    }
    /**
     * 删除数据
     * @param id 分类ID
     * @return
     */
    @RequestMapping(path = "/delete/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public ResponseEntity<Result> delete(@PathVariable("id") String id)
            throws SupperException {

        categorysService.deleteById(id);

        return getResult(null);
    }
}
